<template>
  <div :class="className" :id="id" :style="{height:height,width:width}"></div>
</template>
<script>
import echarts from 'echarts'
import resize from './mixins/resize'

require('echarts/theme/macarons') // echarts theme
export default {
  mixins: [resize],
  props: {
    lineData: {
      type: Object,
      default: {}
    },
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '282px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
  },
  data() {
    return {
      chart: null
    }
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  mounted() {
    this.$nextTick(function () {
      setTimeout(this.initChart(), 200);
    })
  },
  methods: {
    initChart() {
      
      this.chart = echarts.init(document.getElementById(this.id), 'macarons')
      this.chart.clear();//清除画布内容
      this.chart.setOption({
        backgroundColor: '#ffffff',
        title: {
          text: '', //标题
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {   //图例
          top: 0,
          icon: 'rect',
          itemWidth: 14,
          itemHeight: 5,
          itemGap: 13,
          // data: this.lineData.yData, //导航数据
          right: '50%',
          textStyle: {
            fontSize: 12,
            color: '#000000'
          }
        },
        grid: {    //绘图网格
          top: 50,
          left: '1%',
          right: '1%',
          bottom: '2%',
          containLabel: true
        },
        xAxis: [{  //x轴
          data: this.lineData.xAxis,
          boundaryGap: false,
          axisTick: {
            show: false//去掉坐标轴的刻度线
          }
        }],
        yAxis: [{
          type: 'value',
          name: '',
          axisTick: {
            show: false   //去掉坐标轴的刻度线
          },
          axisLabel: {     //坐标轴文字样式
            margin: 10,
            textStyle: {
              fontSize: 14
            }
          },
        },{
          type: 'value',
          name: '',
          position: 'right',
          axisTick: {
            show: false   //去掉坐标轴的刻度线
          },
          axisLabel: {     //坐标轴文字样式
            margin: 10,
            textStyle: {
              fontSize: 14
            }
          },
        }],
        series: [{   //图标类型 数据
                name:'订单数量',
                type: 'line',
                barWidth : 20,
                data: this.lineData.data,
                itemStyle: {
                  normal: {
                    color: '#FF005A',
                    lineStyle: {
                      color: '#FF005A',
                      width: 2
                    }
                  }
                }
            },
            {   //图标类型 数据
                name:'订单金额',
                type: 'line',
                barWidth : 10,
                yAxisIndex: 1,
                data: this.lineData.total,
                itemStyle: {
                  normal: {
                    color: '#3888fa',
                    lineStyle: {
                      color: '#3888fa',
                      width: 2
                    }
                  }
                }
            }
            ]
      })
    }
  },
//折现数据通过变量传递
  watch: {
    lineData: {
      deep: true,
      handler(val) {
        // console.log('折线图监听', val)
        if (val) {
          setTimeout(this.initChart(), 100);
        }
      }
    },
  }
}
</script>
